﻿namespace Example.Views.Shared
{
    using System.Web.Mvc;
    using System.Xml.XPath;

    using Cavity;
    using Cavity.Xml.XPath;

    using RazorGenerator.Testing;

    using Xunit;
    using Xunit.Extensions;

    public sealed class LayoutFacts
    {
        [Fact]
        public void a_definition()
        {
            Assert.True(new TypeExpectations<Layout>()
                            .DerivesFrom<WebViewPage<dynamic>>()
                            .IsConcreteClass()
                            .IsUnsealed()
                            .HasDefaultConstructor()
                            .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Layout());
        }

        [Theory]
        [InlineData("1 = count(/html)")]
        [InlineData("1 = count(/html/head)")]
        [InlineData("1 = count(/html/body)")]
        public void html(string xpath)
        {
            Assert.True(render().Evaluate<bool>(xpath));
        }

        [Theory]
        [InlineData("1 = count(//link)")]
        [InlineData("1 = count(/html/head/link[@href='/Content/screen.css'][@rel='stylesheet'][@type='text/css'])")]
        public void links(string xpath)
        {
            Assert.True(render().Evaluate<bool>(xpath));
        }

        [Theory]
        [InlineData("2 = count(//script)")]
        [InlineData("1 = count(/html/head/script[@src='/Scripts/jquery-1.7.2.min.js'][@type='text/javascript'])")]
        [InlineData("1 = count(/html/head/script[@src='/Scripts/modernizr-2.5.3.js'][@type='text/javascript'])")]
        public void scripts(string xpath)
        {
            Assert.True(render().Evaluate<bool>(xpath));
        }

        [Theory]
        [InlineData("1 = count(//title)")]
        [InlineData("1 = count(/html/head/title[text()='Example : Website'])")]
        [InlineData("1 = count(//h1)")]
        [InlineData("1 = count(//h1[@id='title'][text()='Example'])")]
        public void title(string xpath)
        {
            Assert.True(render("Example").Evaluate<bool>(xpath));
        }

        private static XPathNavigator render()
        {
            // ReSharper disable IntroduceOptionalParameters.Local
            return render("Title");

            // ReSharper restore IntroduceOptionalParameters.Local
        }

        private static XPathNavigator render(string title)
        {
            var view = new Layout();
            view.ViewBag.Title = title;

            return view.RenderAsHtml().CreateNavigator();
        }
    }
}